/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.AllianceDao;
import cz.muni.fi.diplomka.datalayer.entity.AllianceEntity;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class AllianceDaoImpl implements AllianceDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public AllianceDaoImpl() {
    }

    @Override
    public void addAlliance(AllianceEntity allianceEntity) {
        if (allianceEntity == null) {
            throw new IllegalArgumentException("AllianceEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO alliance"
                    + "(name) VALUES"
                    + "(?)";
            ps = con.prepareStatement(insert);

            ps.setString(1, allianceEntity.getName());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editAlliance(AllianceEntity allianceEntity) {
        if (allianceEntity == null) {
            throw new IllegalArgumentException("AllianceEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE alliance SET name=? WHERE id=?";

            ps = con.prepareStatement(update);

            ps.setString(1, allianceEntity.getName());
            ps.setLong(2, allianceEntity.getId());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteAlliance(Long allianceId) {
        if (allianceId == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM alliance WHERE id=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, allianceId);

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public List<AllianceEntity> getAllAlliances() {

        List<AllianceEntity> allianceList = new ArrayList<AllianceEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllAlliances = "SELECT * FROM alliance";

            ps = con.prepareStatement(getAllAlliances);

            rs = ps.executeQuery();

            while (rs.next()) {
                AllianceEntity allianceEntity = new AllianceEntity();
                allianceEntity.setId(rs.getLong("id"));
                allianceEntity.setName(rs.getString("name"));

                allianceList.add(allianceEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return allianceList;
    }

    @Override
    public AllianceEntity getAllianceByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("Name can not be null");
        }
        AllianceEntity allianceEntity = new AllianceEntity();

        try {
            con = DbConnector.getDBConnection();
            String getAllianceByName = "SELECT * FROM alliance WHERE name=?";

            ps = con.prepareStatement(getAllianceByName);

            ps.setString(1, name);

            rs = ps.executeQuery();

            while (rs.next()) {

                allianceEntity.setId(rs.getLong("id"));
                allianceEntity.setName(rs.getString("name"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return allianceEntity;
    }

    @Override
    public AllianceEntity getAllianceOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID cant be null");
        }
        AllianceEntity allianceEntity = new AllianceEntity();

        try {
            con = DbConnector.getDBConnection();
            String getAllianceOfPlayer = "SELECT alliance_id, name FROM alliance_member JOIN alliance ON alliance.id=alliance_member.alliance_id WHERE player_id=?;";

            ps = con.prepareStatement(getAllianceOfPlayer);

            ps.setLong(1, playerId);

            rs = ps.executeQuery();

            while (rs.next()) {
                allianceEntity.setId(rs.getLong("alliance_id"));
                allianceEntity.setName(rs.getString("name"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AllianceDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return allianceEntity;
    }
}
